package cn.filter;

import com.mysql.jdbc.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//过滤器
@WebFilter("/*")
public class CharacterEncodingFilter implements Filter{
	String encoding;
	@Override
	public void destroy() {
		
	}
	//filterChain过滤器链
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
						 FilterChain filterChain) throws IOException, ServletException {
		if(StringUtils.isNullOrEmpty(encoding)){
			encoding="UTF-8";
		}
		request.setCharacterEncoding(encoding);
		response.setCharacterEncoding(encoding);
		response.setContentType("text/html;charset=UTF-8");
		//放行
		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		encoding=filterConfig.getInitParameter("encoding");
	}
}
